package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.AppVersion;
import com.caocaog.market.service.AppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.system.base.entity.groups.UpdateGroup;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * App版本控制管理
 *
 * @author chechen
 * @date 2022-03-01
 */
@RestController
@Api(value = "appVersion", tags = AppVersionController.TABLE_MEANING)
@RequestMapping("/appVersion")
public class AppVersionController extends GenericController<AppVersion, AppVersionService> {

    public static final String TABLE_MEANING = "App版本控制管理";

    @Trim
    @GetMapping
    @AutoLog(value = TABLE_MEANING + PAGE)
    @ApiOperation(value = TABLE_MEANING + PAGE, notes = PAGE)
    public Result<IPage<AppVersion>> queryPage(AppVersion appVersion, QueryRequest queryRequest) {
        appVersion.clearFields();
        Page<AppVersion> page = PageGenerator.createPageSort(queryRequest);
        return Result.OK(super.service.queryAppVersionPage(page, appVersion));
    }

    @Trim
    @PostMapping
    @AutoLog(value = TABLE_MEANING + ADD)
    @ApiOperation(value = TABLE_MEANING + ADD, notes = ADD)
    public Result<String> add(@RequestBody @Validated AppVersion appVersion) {
        appVersion.clearFields();
        appVersion.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());
        return Result.OK(ADD_SUCCESS, super.service.addAppVersion(appVersion));
    }

    @Trim
    @PutMapping
    @AutoLog(value = TABLE_MEANING + EDIT)
    @ApiOperation(value = TABLE_MEANING + EDIT, notes = EDIT)
    public Result<Integer> update(@RequestBody @Validated({UpdateGroup.class}) AppVersion appVersion) {
        appVersion.clearFields();
        appVersion.setUpdateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());
        return Result.OK(EDIT_SUCCESS, super.service.updateAppVersion(appVersion));
    }

    @AutoLog(value = "更改强制更新")
    @ApiOperation(value = "更改强制更新", notes = "更改强制更新")
    @GetMapping(value = "/forcedUpdate/{id}")
    public Result<Integer> forcedUpdate(@PathVariable("id") Integer appVersionId) {
        return Result.OK(super.service.forcedUpdate(appVersionId));
    }

    @AutoLog(value = "设置是否启用")
    @ApiOperation(value = "设置是否启用", notes = "设置是否启用")
    @GetMapping(value = "/using/{id}")
    public Result<Integer> using(@PathVariable("id") Integer appVersionId) {
        return Result.OK(super.service.using(appVersionId));
    }

    @DeleteMapping
    @AutoLog(value = TABLE_MEANING + DELETE)
    @ApiOperation(value = TABLE_MEANING + DELETE, notes = DELETE)
    public Result<Integer> delete(@RequestParam("id") Integer appVersionId) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteAppVersion(appVersionId));
    }

}
